//
//  YHKVisitor.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/2/28.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit
import SnapKit

//设置代理
protocol YHKVisitorDelegate:NSObjectProtocol{
    
    
    //协议方法
    func didLoin()
    func didRegiste()
    
    
    
}





class YHKVisitor: UIView {

//代理的成员属性
    weak var delegate:YHKVisitorDelegate?
    
    
    

//MARK:创建视图三部曲
    
//第一部：init
    override init(frame: CGRect) {
       super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setUpView()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//第二部：创建一个私有方法来添加控件和设置约束
    private func setUpView(){
        
        //添加控件
         addSubview()
       
        //添加约束
        addThtConstraint()
    }
    
//第三部：懒加载
    
    //动画图片
   private lazy var homeImageView:UIImageView = {
        
        //创建视图
        let home = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return home
        
        }()
    
    //中间图片
   private lazy var iconImageView:UIImageView = {
        
        //创建视图
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return icon
        }()
    
    //描述信息
    lazy var describeLabel:UILabel = {
        
        //创建label
        let describe = UILabel()
        
        //属性
        describe.text = "关注一些人，回这里看看有什么惊喜"
        
//        describe.font = UIFont(name: <#T##String#>, size: <#T##CGFloat#>)
        
        describe.font = UIFont.systemFontOfSize(14)//字体大小
        
        describe.textColor = UIColor.darkGrayColor()//颜色
        
        describe.textAlignment = .Center//居中
        
       describe.numberOfLines = 0 //换行
        
        return describe
        
        }()
    
    
    //注册按钮
   private lazy var registerButton:UIButton = {
        
        //创建视图控件
       let register = UIButton(type: UIButtonType.Custom)
        
        //属性
        register.setTitle("注册", forState: UIControlState.Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        register.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //返回
        return register
        }()
    
    //登录按钮
   private lazy var loginButton:UIButton = {
        
        //创建视图控件
        let login = UIButton(type: UIButtonType.Custom)
        
        //属性
        login.setTitle("登陆", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //返回
        return login
        
        
        }()
    
    //遮罩
   private lazy var maskImageView:UIImageView = {
        
        //创建视图控件
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return mask
        }()
    
    
//MARK:两个按钮的点击事件
    func registerClick(){
    
      
        
}
    
    func loginClick(){
    
          delegate?.didLoin()
    }
   
    
    //重载添加控件的方法
   private func addSubview() {
        
        addSubview(homeImageView)
        addSubview(maskImageView)//遮罩
        addSubview(iconImageView)
        addSubview(describeLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    }
    
    //添加约束
   private func addThtConstraint(){
        
        //首页独有图片
        homeImageView.snp_makeConstraints { (make) -> Void in
            
            //控件center和中间一致
            make.center.equalTo(self.snp_center)
            
        }
        
        //公用图片
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            //控件center和中间一致
            make.center.equalTo(self.snp_center)
            
        }
        
        //描述信息
        describeLabel.snp_makeConstraints { (make) -> Void in
            
            //和上方控件相据多少，x和self。centerX 对齐，控件固定宽
            make.top.equalTo(homeImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(216)
        }
        
        
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            
            //上方 ，左边 ，宽，高
            make.top.equalTo(describeLabel.snp_bottom).offset(10)
            make.left.equalTo(describeLabel.snp_left)
            make.height.equalTo(30)
            make.width.equalTo(100)
            
        }
        
        //登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            
            //上，右，宽，高
            make.top.equalTo(describeLabel.snp_bottom).offset(10)
            make.right.equalTo(describeLabel.snp_right)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        
        //遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }

    }
    
    //添加一个动画
   private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")//设置动画
        
        animation.toValue = 2 * M_2_PI //旋转360
        
        animation.repeatCount = MAXFLOAT//动画重复的次数
        
        animation.duration = 5 //动画持续的时间
        
        animation.removedOnCompletion = false//保证动画不被移除
        
        homeImageView.layer.addAnimation(animation, forKey: nil)//添加动画
        
    }
    
    
    //写一个方法，让外界访问
    func setInfo(miaoShu:String,image:String,animation:Bool){
        
        describeLabel.text = miaoShu
        iconImageView.image = UIImage(named: image)
        
        //当满足该条件的时候
        if animation{
            
            homeImageView.hidden = false//让动画图片不隐藏
            startAnimation()//执行动画
            
        }else{
            homeImageView.hidden = true
        }
        
        
    }
    
    
    
    
    

}
